const EssayModel = require('../do/models/essay'),
    Type = require('../do/models/type')
class EssayService {
    async getEssay(tid, offset, limit) {
        if (!tid) {
            return await EssayModel.findAll({
                limit: limit,
                offset: offset,
                include: {
                    association:
                        EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
                },

            }
            );
        }
        return await EssayModel.findOne({
            where: {
                id: tid
            },
            include: {
                association:
                    EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
            }
        }
        );
    }
    async createEssay(data) {
        return await EssayModel.create(data);
    }
    async destroyEssay(id) {
        return await EssayModel.destroy({
            where: {
                id
            }
        })

    }
    async updateEssay(id, data) {
        return await EssayModel.update({
            data
        }, {
            where: {
                id
            }
        })
    }
    async changeEssayStatus(id, status) {
        return await EssayModel.update({
            status
        }, {
            where: {
                id
            }
        })
    }
}
module.exports = new EssayService();